///Table 1,2,A11,A14 final run 


log using "$rootpath/log/Table_1_2_A11_A14Results.log", replace
use "$rootpath/processed_data/Estimation_Data.dta", clear
drop if key_estimation_sample!=1
set seed 99164
sort frsnumber year, stable
quietly by frsnumber year:  gen dup = cond(_N==1,0,_n)
gen duplicated=0
replace duplicated=1 if dup>1
gsort -duplicated
gen test_n=_n
summarize frsnumber
replace frsnumber=r(max)*10000+test_n if dup>1
drop dup
sort frsnumber year, stable
quietly by frsnumber year:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup
merge 1:1 frsnumber year using "$rootpath/processed_data/estimation_ICIS_plant_file.dta"
drop if _merge==2
drop _merge
foreach var in quarterly_penaltiesnofed annual_quarterly_tests2 quarterly_full_inspectionsnofed quarterly_part_inspectionsnofed annual_full_inspec annual_part_inspec annual_quarterly_penalties2 annual_tot_hpv2 annual_tot_frv2 {
replace `var'=0 if `var'==.
}
replace tot_damage=tot_damage/1000000
gen wes_inspec=annual_part_inspec+annual_full_inspec
gen wes_state_inspec=quarterly_full_inspectionsnofed+quarterly_part_inspectionsnofed
gen wes_fed_inspec=wes_inspec - wes_state_inspec
gen wes_fed_penal=annual_quarterly_penalties2 - quarterly_penaltiesnofed

gen comp_wes=1
replace comp_wes=0 if (annual_tot_hpv2>0&annual_tot_hpv2!=.)|(annual_tot_frv2>0&annual_tot_frv2!=.)
rename post treatment
balancetable treatment tot_damage tot_emissions annual_quarterly_penalties2 wes_inspec comp_wes annual_tot_hpv2 annual_tot_frv2 using "$rootpath/table_graph/Table_1.tex", pvalues replace 

///Table 2 final run 
drop if annual_tot_hpv2==0&annual_tot_frv2==0
replace treatment=. if annual_tot_hpv2==0
foreach var in annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec {
gen `var'_orig=`var'
replace `var'=`var'/annual_tot_hpv2
}

//all plants HPV
balancetable treatment annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec using "$rootpath/table_graph/COVTable_HPV.tex", pvalues replace 
drop treatment
gen treatment=1 if year>2014&annual_tot_hpv2==0
replace treatment=0 if year<2015&annual_tot_hpv2==0
foreach var in annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec {

replace `var'=`var'_orig/annual_tot_frv2
}

//all plants FRV
balancetable treatment annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec using "$rootpath/table_graph/COVTable_FRV.tex", pvalues replace 

use "$rootpath/processed_data/Estimation_Data.dta", clear
drop if key_estimation_sample!=1
set seed 99164
sort frsnumber year, stable
quietly by frsnumber year:  gen dup = cond(_N==1,0,_n)
gen duplicated=0
replace duplicated=1 if dup>1
gsort -duplicated
gen test_n=_n
summarize frsnumber
replace frsnumber=r(max)*10000+test_n if dup>1
drop dup
sort frsnumber year, stable
quietly by frsnumber year:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup
merge 1:1 frsnumber year using "$rootpath/processed_data/estimation_ICIS_plant_file.dta"
drop if _merge==2
drop _merge
foreach var in quarterly_penaltiesnofed annual_quarterly_tests2 quarterly_full_inspectionsnofed quarterly_part_inspectionsnofed annual_full_inspec annual_part_inspec annual_quarterly_penalties2 annual_tot_hpv2 annual_tot_frv2 {
replace `var'=0 if `var'==.
}
replace tot_damage=tot_damage/1000000
gen wes_inspec=annual_part_inspec+annual_full_inspec
gen wes_state_inspec=quarterly_full_inspectionsnofed+quarterly_part_inspectionsnofed
gen wes_fed_inspec=wes_inspec - wes_state_inspec
gen wes_fed_penal=annual_quarterly_penalties2 - quarterly_penaltiesnofed

gen comp_wes=1
replace comp_wes=0 if (annual_tot_hpv2>0&annual_tot_hpv2!=.)|(annual_tot_frv2>0&annual_tot_frv2!=.)
rename post treatment
gen treat=1 if avg_old_code_prob>0.53
replace treat=0 if avg_old_code_prob<0.23
tab treat
keep if treat==0&treat!=.

sort frsnumber year
egen num_obs=nvals(year), by(frsnumber)
//below is for table a14
balancetable treatment tot_damage tot_emissions annual_quarterly_penalties2 wes_inspec comp_wes annual_tot_hpv2 annual_tot_frv2 using "$rootpath/table_graph/Table_A14_low.tex", pvalues replace 
drop if annual_tot_hpv2==0&annual_tot_frv2==0
drop treatment
gen treatment=1 if year>2014&annual_tot_hpv2>0
replace treatment=0 if year<2015&annual_tot_hpv2>0
foreach var in annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec {
gen `var'_orig=`var'
replace `var'=`var'/annual_tot_hpv2
}

//bottom quintile HPV
balancetable treatment annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec using "$rootpath/table_graph/COVTable_HPV_low.tex", pvalues replace 
drop treatment
gen treatment=1 if year>2014&annual_tot_hpv2==0
replace treatment=0 if year<2015&annual_tot_hpv2==0
foreach var in annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec {
replace `var'=`var'_orig/annual_tot_frv2
}

//bottom quintile FRV
balancetable treatment annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec using "$rootpath/table_graph/COVTable_FRV_low.tex", pvalues replace 


use "$rootpath/processed_data/Estimation_Data.dta", clear
drop if key_estimation_sample!=1
set seed 99164
sort frsnumber year, stable
quietly by frsnumber year:  gen dup = cond(_N==1,0,_n)
gen duplicated=0
replace duplicated=1 if dup>1
gsort -duplicated
gen test_n=_n
summarize frsnumber
replace frsnumber=r(max)*10000+test_n if dup>1
drop dup
sort frsnumber year, stable
quietly by frsnumber year:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup
merge 1:1 frsnumber year using "$rootpath/processed_data/estimation_ICIS_plant_file.dta"
drop if _merge==2
drop _merge
foreach var in quarterly_penaltiesnofed annual_quarterly_tests2 quarterly_full_inspectionsnofed quarterly_part_inspectionsnofed annual_full_inspec annual_part_inspec annual_quarterly_penalties2 annual_tot_hpv2 annual_tot_frv2 {
replace `var'=0 if `var'==.
}
replace tot_damage=tot_damage/1000000
gen wes_inspec=annual_part_inspec+annual_full_inspec
gen wes_state_inspec=quarterly_full_inspectionsnofed+quarterly_part_inspectionsnofed
gen wes_fed_inspec=wes_inspec - wes_state_inspec
gen wes_fed_penal=annual_quarterly_penalties2 - quarterly_penaltiesnofed

gen comp_wes=1
replace comp_wes=0 if (annual_tot_hpv2>0&annual_tot_hpv2!=.)|(annual_tot_frv2>0&annual_tot_frv2!=.)
rename post treatment
gen treat=1 if avg_old_code_prob>0.53
replace treat=0 if avg_old_code_prob<0.23
tab treat
keep if treat==1&treat!=.
sort frsnumber year
egen num_obs=nvals(year), by(frsnumber)
//below is for table a14
balancetable treatment tot_damage tot_emissions annual_quarterly_penalties2 wes_inspec comp_wes annual_tot_hpv2 annual_tot_frv2 using "$rootpath/table_graph/Table_A14_high.tex", pvalues replace 
drop if annual_tot_hpv2==0&annual_tot_frv2==0
drop treatment
gen treatment=1 if year>2014&annual_tot_hpv2>0
replace treatment=0 if year<2015&annual_tot_hpv2>0
foreach var in annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec {
gen `var'_orig=`var'
replace `var'=`var'/annual_tot_hpv2
}
balancetable treatment annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec using "$rootpath/table_graph/COVTable_HPV_high.tex", pvalues replace 
drop treatment
gen treatment=1 if year>2014&annual_tot_hpv2==0
replace treatment=0 if year<2015&annual_tot_hpv2==0
foreach var in annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec {
replace `var'=`var'_orig/annual_tot_frv2
}
balancetable treatment annual_quarterly_penalties2 wes_fed_penal quarterly_penaltiesnofed wes_inspec wes_fed_inspec wes_state_inspec using "$rootpath/table_graph/COVTable_FRV_high.tex", pvalues replace 

log close
